package com.example.meterialproject.activitys

import android.os.Bundle
import android.util.Log
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.view.forEachIndexed
import com.example.meterialproject.base.BaseActivity
import com.example.meterialproject.R
import com.example.meterialproject.databinding.ActivityMaterialButtonBinding
import com.example.meterialproject.exts.*
import com.example.meterialproject.exts.szj.szj
import com.google.android.material.button.MaterialButton


/**
 *
 * @ClassName: MaterialButtonActivity
 * @Author: android 超级兵
 * @CreateDate: 1/6/22$ 2:12 PM$
 * TODO materialButton页面
 * MaterialButtonToggleGroup 参考文档: https://developer.android.com/reference/com/google/android/material/button/MaterialButtonToggleGroup
 */
class MaterialButtonActivity : BaseActivity<ActivityMaterialButtonBinding>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        /*
         * 作者:android 超级兵
         * 创建时间: 1/7/22 8:09 PM
         * TODO switchMaterial点击
         */
        binding.switchMaterial.setOnCheckedChangeListener { _, isChecked ->
            binding.switchMaterial.text = if (!isChecked) "水平" else "垂直"

            val orientation = if (isChecked)
                                    LinearLayout.VERTICAL
                                else
                                    LinearLayout.HORIZONTAL

            // 设置水平还是垂直
            binding.materialButtonGroup2.orientation = orientation

            // 循环所有child
            binding.materialButtonGroup2.forEachIndexed { index, _ ->
                // 判断是否是垂直
                val inset: Int = getInsetForOrientation(isChecked.takeTrue()).toInt()

                val childView = binding.materialButtonGroup2.getChildAt(index)

                if (childView is MaterialButton) {
                    // 设置间距
                    childView.insetBottom = inset
                    childView.insetTop = inset
                }
            }
        }

        /*
         * 作者:android 超级兵
         * 创建时间: 1/8/22 10:47 AM
         * 参考文档: https://developer.android.com/reference/com/google/android/material/button/MaterialButtonToggleGroup
         *
         * TODO addOnButtonCheckedListener参数介绍:
         *          参数一: 当前 MaterialButtonToggleGroup 对象
         *          参数二: 点击的id
         *          参数三: 当前点击是否选中
         */
        Log.i(TAG, "========= 更改主题色,界面重新绘制了 =======")

        binding.materialButtonGroup1.addOnButtonCheckedListener { _, checkedId, isChecked ->
            // 选中
            if (isChecked) {
                // 这里会重复 toast 是因为改变主题会重新绘制activity。。。
                findViewById<MaterialButton>(checkedId).text toast this
                when (checkedId) {
                    R.id.bt1 -> {
                        AppCompatDelegate.setDefaultNightMode(
                            AppCompatDelegate.MODE_NIGHT_YES
                        )
                    }
                    R.id.bt2 -> {
                        AppCompatDelegate.setDefaultNightMode(
                            AppCompatDelegate.MODE_NIGHT_NO
                        )
                    }
                    R.id.bt3 -> {
                        AppCompatDelegate.setDefaultNightMode(
                            AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
                        )
                    }
                }
            }
            Log.i(TAG, "isChecked:${isChecked}-${findViewById<MaterialButton>(checkedId).text}")
        }

        /*
        * 作者:android 超级兵
        * 创建时间: 1/8/22 10:50 AM
        */
        //region 系统颜色改变 单个点击
        /*
        binding.bt1.click {
            AppCompatDelegate.setDefaultNightMode(
                AppCompatDelegate.MODE_NIGHT_YES
            )
        }
        binding.bt2.click {
            AppCompatDelegate.setDefaultNightMode(
                AppCompatDelegate.MODE_NIGHT_NO
            )
        }
        binding.bt3.click {
            AppCompatDelegate.setDefaultNightMode(
                AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
            )
        }
        */
        //endregion
        binding.materialButtonGroup2.addOnButtonCheckedListener { group, checkedId, isChecked ->
            // group.checkedButtonIds 获取到当前选中的 id (int)->int
            if (isChecked) {
                group.checkedButtonIds.map {
                    findViewById<MaterialButton>(it)
                }.map {
                    it.text
                }.toString() toast this
            }
            Log.i(TAG, "isChecked:${isChecked}-${findViewById<MaterialButton>(checkedId).text}")

            Log.i(TAG, "checkedButtonIds:${group.checkedButtonIds}")

            Log.i(TAG, "是否是单选:${group.isSingleSelection}")
        }

        /*
         * 作者:android 超级兵
         * 创建时间: 1/8/22 11:09 AM
         * TODO MaterialButtonToggleGroup extends LinearLayout 所以可以直接addView
         */

        val materialButtonList = listOf(
            MaterialButton(
                this,
                null,
                R.attr.materialButtonOutlinedStyle // 动态设置样式
            ).apply { text = "历史" },
            MaterialButton(
                this,
                null,
                R.attr.materialButtonOutlinedStyle // 动态设置样式
            ).apply { text = "化学" },
            MaterialButton(
                this,
                null,
                R.attr.materialButtonOutlinedStyle // 动态设置样式
            ).apply { text = "音乐" },
            MaterialButton(
                this,
                null,
                R.attr.materialButtonOutlinedStyle // 动态设置样式
            ).apply { text = "美术" },
        )

        // 动态添加
        materialButtonList.forEach {
            binding.materialButtonGroup2.addView(it)
        }
    }

    /*
     * 作者:android 超级兵
     * 创建时间: 1/7/22 5:45 PM
     * TODO 判断是否是垂直
     */
    private fun getInsetForOrientation(orientation: Int) =
        if (orientation == LinearLayout.VERTICAL) 0f else szj.toDp(6f)
}




